import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutterlogistics/ui/pages/home/application_page.dart';
import 'package:flutterlogistics/ui/pages/home/mine_page.dart';
import 'package:flutterlogistics/ui/pages/home/statistics_page.dart';
import 'package:flutterlogistics/ui/pages/home/work_page.dart';
import 'package:flutterlogistics/utils/common_util.dart';
import 'package:flutterlogistics/utils/log_util.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';

class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return HomeState();
  }
}

class HomeState extends State<HomePage> {

  int _selectedIndex = 0;

  List<Widget> pages = <Widget>[
    WorkPage(),
    ApplicationPage(),
    StatisticsPage(),
    MinePage()
  ];

  Future<void> getSDCardDir() async {
    CommonUtil().sDCardDir = (await getExternalStorageDirectory()).path;
  }

  // Permission check
  Future<void> getPermission() async {
    if (Platform.isAndroid) {
      PermissionStatus permission = await PermissionHandler()
          .checkPermissionStatus(PermissionGroup.storage);
      if (permission != PermissionStatus.granted) {
        await PermissionHandler().requestPermissions([PermissionGroup.storage]);
      }
      await getSDCardDir();
    } else if (Platform.isIOS) {
      await getSDCardDir();
    }
  }

  @override
  void initState() {
    super.initState();
    getPermission();
  }

  @override
  Widget build(BuildContext context) {
    var _pageController = PageController();
    // 上次点击时间
    DateTime _lastPressed;
    return Scaffold(
      body: WillPopScope(
        onWillPop: () async {
          if (_lastPressed == null ||
              DateTime.now().difference(_lastPressed) > Duration(seconds: 1)) {
            //两次点击间隔超过1秒则重新计时
            _lastPressed = DateTime.now();
            return false;
          }
          return true;
        },
        child: PageView.builder(
          itemBuilder: (ctx, index) => pages[index],
          itemCount: pages.length,
          controller: _pageController,
          physics: NeverScrollableScrollPhysics(),
          onPageChanged: (index) {
            setState(() {
              _selectedIndex = index;
              LogUtil.e(_selectedIndex, tag: "HomePage");
            });
          },
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.format_list_bulleted),
            title: Text('工作'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.dvr),
            title: Text('应用'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.group_work),
            title: Text('统计'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.person_pin),
            title: Text('我的'),
          )
        ],
        currentIndex: _selectedIndex,
        onTap: (index) {
          _pageController.jumpToPage(index);
        },
      ),
    );
  }
}
